package kosta.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import kosta.model.board.dis.DisBoardService;
import kosta.model.board.dis.DisBoardVO;
import kosta.model.reply.dis.DisReplyService;
import kosta.model.reply.dis.DisReplyVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class DisBoardController {
	@Resource(name="disBoardServiceImpl")
	private DisBoardService disBoardService;
	@Resource(name="disReplyServiceImpl")
	private DisReplyService disReplyService;
	
	/**
	 * 토론 게시판 상세보기
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_disView.do")
	public ModelAndView disView() throws Exception {
		//System.out.println("disboard gogo~");
		DisBoardVO dvo = disBoardService.disView();
		List<DisReplyVO> list = disReplyService.getDisReplyList(dvo.getDisNo());
		//System.out.println(list+" con reply");
		HashMap map = new HashMap();
		map.put("dvo", dvo);
		map.put("list", list);
		//System.out.println(map);
		return new ModelAndView("dis_view.disboard","map",map);
	}
	
	/**
	 * 토론게시판 검색(날짜로 검색)
	 * @param no
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_disDatePick.do")
	@ResponseBody
	public HashMap disDatePick(String no) throws Exception {
		DisBoardVO dvo = disBoardService.selectByDisNo(no);
		List<DisReplyVO> list = disReplyService.getDisReplyList(no);
		HashMap map = new HashMap();
		map.put("dvo", dvo);
		map.put("list", list);
		System.out.println(map+" datepick");
		return map;
	}
	
	/**
	 * 토론게시판 내용 검색
	 * @param disContent
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_findDisContent.do")
	public ModelAndView findDisContent(String disContent) throws Exception {
		System.out.println("findDisContent "+disContent);
		List<DisBoardVO> list = disBoardService.findDisContent(disContent);
		return new ModelAndView("dis_findResult.disboard","list",list);
	}
	
	/**
	 * 토론게시판 글쓰기 페이지로 이동
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_goDisWrite.do")
	public String goDisWrite() throws Exception {
		System.out.println("DisWrite go go~");
		return "dis_write.disboard";
	}
	
	/**
	 * 토론게시판 글쓰기 등록
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_insertDisBoard.do")
	public ModelAndView insertDisBoard(DisBoardVO vo) throws Exception{
		System.out.println("DisInsert");
		disBoardService.registerDisBoard(vo);
		List<DisBoardVO> list = disBoardService.getDisList();
		return new ModelAndView("dis_admin.disboard","list",list);
	}
	
	/**
	 * 토론게시판 업데이트 페이지로 가는 메서드
	 * @param disNo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_goDisUpdate.do")
	public ModelAndView goDisModify(String disNo) throws Exception{
		System.out.println("DisUpdate go go~");
		DisBoardVO dvo = disBoardService.selectByDisNo(disNo);
		System.out.println(dvo+" con");
		return new ModelAndView("dis_update.disboard","dvo",dvo);
	}
	
	/**
	 * 토론게시판 업데이트
	 * @param vo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_updateDisBoard.do")
	public ModelAndView updateDisBoard(DisBoardVO vo) throws Exception {
		System.out.println("DisUpdate");
		disBoardService.updateDisBoard(vo);
		List<DisBoardVO> list = disBoardService.getDisList();
		return new ModelAndView("dis_admin.disboard","list",list);
	}
	
	/**
	 * 토론게시판 삭제
	 * @param disNo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_goDisDelete.do")
	public ModelAndView deleteDisBoard(String disNo) throws Exception {
		System.out.println("DisDelete");
		disBoardService.erasureDisBoard(disNo);
		List<DisBoardVO> list = disBoardService.getDisList();
		return new ModelAndView("dis_admin.disboard","list",list);
	}
	
	/**
	 * 토론게시판 관리자 페이지로 가는 메서드
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_goDisAdmin.do")
	public ModelAndView goDisAdmin() throws Exception {
		System.out.println("DisAdmin");
		List<DisBoardVO> list = disBoardService.getDisList();
		System.out.println("con list"+list);
		return new ModelAndView("dis_admin.disboard","list",list);
	}
	
	/**
	 * 토론게시판 댓글쓰기
	 * @param drvo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_insertDisReply.do")
	public ModelAndView insertDisReply(DisReplyVO drvo) throws Exception{
		//System.out.println("insertDisReply : "+drvo);
		disReplyService.insertDisReply(drvo);
		//System.out.println("insertDisReply2");
		DisBoardVO dvo = disBoardService.selectByDisNo(drvo.getDisReRef());
		List<DisReplyVO> list = disReplyService.getDisReplyList(dvo.getDisNo());
		HashMap map = new HashMap();
		map.put("dvo", dvo);
		map.put("list", list);
		//System.out.println(map);
		return new ModelAndView("dis_view.disboard","map",map);
	}
	
	/**
	 * 토론게시판 댓글삭제
	 * @param disReNo
	 * @param disNo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_deleteDisReply.do")
	public ModelAndView deleteDisReply(int disReNo,String disNo,String id) throws Exception {
		//System.out.println("deleteDisReply : "+disReNo+" "+disNo);
		disReplyService.deleteDisReply(disReNo, id);
		DisBoardVO dvo = disBoardService.selectByDisNo(disNo);
		List<DisReplyVO> list = disReplyService.getDisReplyList(disNo);
		HashMap map = new HashMap();
		map.put("dvo", dvo);
		map.put("list", list);
		return new ModelAndView("dis_view.disboard","map",map);
	}
	
	/**
	 * 토론게시판 댓글수정
	 * @param drvo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/board_updateDisReply.do")
	public ModelAndView updateDisReply(DisReplyVO drvo) throws Exception {
		//System.out.println(drvo+" con");
		disReplyService.updateDisReply(drvo);
		DisBoardVO dvo = disBoardService.selectByDisNo(drvo.getDisReRef());
		List<DisReplyVO> list = disReplyService.getDisReplyList(drvo.getDisReRef());
		HashMap map = new HashMap();
		map.put("dvo", dvo);
		map.put("list", list);
		return new ModelAndView("dis_view.disboard","map",map);
	}
}
